package com.shangqing.system.service.hail.impl;
import com.shangqing.common.core.domain.Result;
import com.shangqing.common.core.domain.entity.geologic.Citys;
import com.shangqing.system.mapper.Hail.HailDisasterMapper;
import com.shangqing.system.mapper.geology.CitysMapper;
import com.shangqing.system.service.hail.HailDisasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class HailDisasterServiceImpl implements HailDisasterService {
    @Autowired
    HailDisasterMapper hailDisasterMapper;
    @Autowired
    CitysMapper citysMapper;
    @Override
    public List findAllHailList(String Num) {
        List<Map> list=hailDisasterMapper.findAllHailList(Num);
        if(list !=null){
            return list;
        }
        return null;
    }

    @Override
    public Integer findHailQuarterList(String year, String quarter) {
        return hailDisasterMapper.findHailQuarterList(year, quarter);
    }

    @Override
    public List findCountHailByCity(String year, String quarter) {
        List countlist=new ArrayList();
        List<Integer> list = citysMapper.findAllCity();
        Integer count=hailDisasterMapper.findHailQuarterList(year, quarter);
        System.out.println("查询灾害总数为：===================" + count);
        //百分比小数点控制
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(0);
        if (list != null) {
            for (Integer id : list) {
                // 根据区级查有多少条灾害信息，可以按照年、季度进行查
                int num=hailDisasterMapper.findCountHailByCity(id,year,quarter);
                //区级名称
                Citys city = citysMapper.findCityById(id);
                System.out.println("====当前id为：======" + id + "===当前id下的灾害条数为====" + num);
                if (num != 0 && count != 0) {
                    String baifenbi = numberFormat.format((float) num / (float) count * 100) + "%";
                    System.out.println("====" + num + "和总数" + count + "的百分比为:" + baifenbi);
                    Result result=new Result();
                    result.setCityName(city.getCityName());
                    result.setProportion(baifenbi);
                    countlist.add(result);
                } else {
                    Result result=new Result();
                    result.setCityName(city.getCityName());
                    result.setProportion("0%");
                    countlist.add(result);
                }

            }
        }
        return countlist;
    }

    @Override
    public List findRegionalDisasterStatistics() {
        List<Map> list=hailDisasterMapper.findRegionalDisasterStatistics();
        return list;
    }
}
